﻿#region Copyright ©2008-2011, Technische Universitaet Muenchen
// ====================================================================================================
//
// Last Changed by       $Author: then $
// Last Changed Date     $LastChangedDate: 2009-05-16 14:58:56 +0200 (Sat, 16 May 2009) $
// Last Changed Revision $Rev: 144 $
//
// ====================================================================================================
#endregion

using System.Xml;
using Tum.CollabXT.TFS.Resources;

namespace Tum.CollabXT.TFS
{
    internal class ProcessTemplate : IProcessor
    {
        public bool Process(TemplateProcessor processor)
        {
            processor.ToolProvider.ProcessLog.AddEntry(Language.Processing_ProjectTemplate);
            string processTemplateFileName = processor.ToolProvider.OutputPath + "ProcessTemplate.xml";

            #region Load process template document
            processor.ToolProvider.ProcessLog.AddEntry(Language.ProcessTemplate_Loading);
            XmlDocument processTemplateDoc = new XmlDocument();
            try
            {
                processTemplateDoc.Load(processTemplateFileName);
            }
            catch
            {
                processor.ToolProvider.ProcessLog.AddEntry(string.Format(Language.Error_LoadingFailed, processTemplateFileName), LogEntryType.Error);
                return false;
            }
            #endregion

            processor.ToolProvider.ProcessLog.AddEntry(Language.ProcessTemplate_SelectingMetadataNode);
            XmlNode templateMetadataNode = processTemplateDoc.SelectSingleNode("/ProcessTemplate/metadata");
            if (templateMetadataNode != null)
            {

                //Get project title and save to template
                processor.ToolProvider.ProcessLog.AddEntry(Language.ProcessTemplate_WritingTitle);
                string projectTitle = processor.ProcessProvider.Name;
                if (projectTitle == null)
                    projectTitle = string.Empty;
                XmlNode metadataNameNode = templateMetadataNode.SelectSingleNode("name");
                if(metadataNameNode != null)
                    metadataNameNode.InnerText = Helper.PrepareStringForTFS(projectTitle, Helper.TFS_MaxWorkItemTextLen);

                //Get description
                processor.ToolProvider.ProcessLog.AddEntry(Language.ProcessTemplate_WritingDescription);
                string projectDescription = processor.ProcessProvider.Description;
                if (projectDescription == null)
                    projectDescription = string.Empty;
                XmlNode metadataDescNode = templateMetadataNode.SelectSingleNode("description");
                if(metadataDescNode != null)
                    metadataDescNode.InnerText = Helper.PrepareStringForTFS(projectDescription, int.MaxValue);
            }

            #region Save process template
            processor.ToolProvider.ProcessLog.AddEntry(Language.ProcessTemplate_Saving);
            try
            {
                processTemplateDoc.Save(processTemplateFileName);
            }
            catch
            {
                processor.ToolProvider.ProcessLog.AddEntry(string.Format(Language.Error_SavingFailed, processTemplateFileName), LogEntryType.Error);
                return false;
            }
            #endregion

            return true;
        }

        #region Dummy implementations of file and folder properties for IProcessor. Must not be called.
        public string TaskFileFolder
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
                throw new System.NotImplementedException();
            }
        }

        public string TaskFileName
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
                throw new System.NotImplementedException();
            }
        } 
        #endregion
    }
}
